<?php

namespace App\Http\Controllers\user;

use App\Dao\BillDao;
use App\Dao\OrdersDao;
use App\Dao\UsersDao;
use App\Dao\UserStockDao;
use App\Exceptions\SysExceptions;
use App\Services\Code;
use App\Services\OssServices;
use EasyWeChat\Kernel\Exceptions\HttpException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use Psr\SimpleCache\InvalidArgumentException;

class UsersController extends BaseController
{
    /**
     * @return array
     * @throws InvalidArgumentException
     * @throws SysExceptions
     * @throws HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws InvalidConfigException
     * @throws RuntimeException
     */
    public function login(): array
    {
        $params = request()->all();
        $data = UsersDao::login($params);
        return Code::message('',200,$data);
    }


    public function getUserInfo(): array
    {
        $user_id = getUserToken();
        $data = UsersDao::getUserInfo([
            'user_id'=>$user_id
        ]);
        return Code::message('',200,$data);
    }


    public function get_spread(){
        return Code::message('',200,UsersDao::get_user_builder([
            'spread_id'=>getUserToken()
        ])->get());
    }


    public function set_address_json_data(): array
    {
        $params = array_merge(request()->all(),[
            'user_id'=>getUserToken(),
        ]);
        UsersDao::set_json_city_address($params);
        return Code::message();
    }


    public function get_user_stock(): array
    {
        return Code::message('',200,UserStockDao::get_user_stock([
            'user_id'=>getUserToken()
        ]));
    }



    public function all_stock(){
        return Code::message('',200,UserStockDao::all_stock([
            'user_id'=>getUserToken()
        ]));
    }


    public function set_card_json(): array
    {
        UsersDao::set_card_json(array_merge([
            'user_id'=>getUserToken()
        ],request()->all()));
        return Code::message('',200);
    }

    public function widthwal(): array
    {
        $params = array_merge(request()->all(),[
            'user_id'=>getUserToken()
        ]);
        UsersDao::widthwal($params);
        return Code::message();
    }

    /**
     * Created by PhpStorm.
     * @purpose 获取余额变动
     * @Author: admin
     * @Time: 2023/5/14 12:42
     */
    public function get_bills(): array
    {
        $all = array_merge(request()->all(),[
            'user_id'=>getUserToken()
        ]);
        return Code::message('',200,BillDao::get_bills($all));
    }


    public function get_order_child_info_be_hexiao(): array
    {
        $params = request()->all();
        return Code::message('',200,OrdersDao::get_user_order_info([
            'order_child_id'=>request('order_child_id')
        ]));
    }


    public function update_nickname_headimgurl(): array
    {
        $params = array_merge(request()->all(),[
            'user_id'=>getUserToken()
        ]);
        UsersDao::update_nickname_headimgurl($params);
        return Code::message('',200);
    }

    public function file_up(): array
    {
        $filePath = $_FILES["file"]["tmp_name"];
        $ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上传文件后缀
        $object=date('YmdHis').'-'.time().'.'.$ext;
        $oss = new OssServices();
        $url = $oss->upload($object,$filePath);
        return Code::message('',200,[
            'url'=>$url
        ]);

    }



    public function get_now_money_lists(): array
    {
        return Code::message('',200,UsersDao::get_now_money_prices());
    }




}
